package com.ashin.user.controller;

import com.ashin.user.dto.UserPageDTO;
import com.ashin.user.entity.User;
import com.ashin.user.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserMapper userMapper;

    /**
     * 租户查询列表
     *
     * @return
     */
    @GetMapping(value = "/user")
    public List<User> queryUserList() {
        List<User> users = userMapper.selectList(null);
        return users;
    }

    /**
     * 租户查询列表
     *
     * @return
     */
    @PostMapping(value = "/page")
    public Page<User> queryUserPageList(@RequestBody UserPageDTO userPageDTO) {
        Page<User> page = new Page<>(userPageDTO.getPageNo(),userPageDTO.getPageSize());
        page = userMapper.selectPage(page, null);

        return page;
    }

    /**
     * 租户添加
     */
    @PostMapping(value = "/user")
    public void addUserList() {
        userMapper.insert(User.builder().age(12).name("zjh").email("zjhptxl@163.com").build());
    }

    /**
     * 租户逻辑删除
     *
     * @param id
     */
    @DeleteMapping(value = "/user/{id}")
    public void deleteUser(@PathVariable("id") Integer id) {
        userMapper.deleteById(id);
    }

    /**
     * 租户添加
     */
    @PutMapping(value = "/user/{id}")
    public void putUserList(@PathVariable Long id) {
        userMapper.updateById(User.builder()
                .id(id)
                .age(12).name("zjh")
                .email("zjhptxl@163.com")
                .build());
    }
}
